<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>恒铸智能柜管理平台</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/resources/layui/css/layui.css"
	media="all" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/resources/css/zTreeStyle.css"
	type="text/css" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/resources/css/demo.css"
	type="text/css" />
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
<script type="text/javascript">
			var permissionIds="";
			layui.use(['jquery','layer', 'form'], function(){
			    var form = layui.form,
			        layer = parent.layer === undefined ? layui.layer : top.layer
	        		var $ = layui.$;
				  /*   zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes); */
			    //
			     form.on('submit(addForm)',function(data){
					//  $.get("/main/addRole?des="+$(".des").val()+"&roleName="+$(".roleName").val()+"&permissions="+permissionIds+"&compId="+$(".comp").val(),
					// 		 function(rest){
					// 	 if(rest.success){
					// 		 layer.msg("添加成功");
					// 		 parent.window.location.reload();
					// 	 }else{
					// 		 layer.msg(rest.message);
					// 	 }
				 	// })
					 $.ajax({
						 type: 'POST',
						 url: "/main/addRole",
						 data: {
							 des:			$(".des").val(),
							 roleName:		$(".roleName").val(),
							 permissions:	permissionIds,
							 compId:		$(".comp").val(),
							 percent:		$(".percent").val(),

						 },
						 success: function(rest){
							 if(rest.success){
								 layer.msg("添加成功");
								 parent.window.location.reload();
							 }else{
								 layer.msg(rest.message);
							 }
						 },
						 contentType:'application/x-www-form-urlencoded; charset=UTF-8'
					 });
			 	})
			})
			
			var setting = {};
			$.get("/main/getRoleTree", function(data) {
				zNodes = data
				//正常加载信息
				if(window.sessionStorage.getItem("asd")) {
					var asd = window.sessionStorage.getItem("asd");
					zNodes = JSON.parse(asd).concat(zNodes); //转换成JSON格式
				}
			
				var setting = {
					check: {
						enable: true
					},
					data: {
						key: {
							title: "t"
						},
						simpleData: {
							enable: true
						}
					},
					callback: {
						onMouseDown: onMouseDown,
						onCheck:onCheck
					}
				};
			
				var log;
			
				function onMouseDown(event, treeId, treeNode) {
					showLog((treeNode ? treeNode.name : "root"), "down");
				}
			
				function showLog(str, logType) {
					log = $("#log" + "_" + logType);
					log.append("<li>" + str + "</li>");
					if(log.children("li").length > 1) {
						log.get(0).removeChild(log.children("li")[0]);
					}
				}
			
				var treeObj;
				$(document).ready(function() {
					treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
					
					//防止没改权限就点提交使权限变成空
					nodes=treeObj.getCheckedNodes(true);
					for(var i=0;i<nodes.length;i++){
				    	if(null != nodes[i].id){
				    		permissionIds+=nodes[i].id + ",";
				    	}
				    }
				    return permissionIds;
				});
				function onCheck(e,treeId,treeNode){
				    nodes=treeObj.getCheckedNodes(true);
				    
				    //防止重复
				    permissionIds="";
				    for(var i=0;i<nodes.length;i++){
				    	if(null != nodes[i].id){
				    		permissionIds+=nodes[i].id + ",";
				    	}
				    }
				    return permissionIds;
			    }
			})
	</script>


</head>

<body>
	<div class="layui-layout layui-layout-admin"
		style="padding-left: 40px; margin-top: 20px;">
		<form class="layui-form" action="" onsubmit="javascript:;" name="form"
			method="post">
			<div class="content_wrap">
				<div class="zTreeDemoBackground left">
					<ul id="treeDemo" class="ztree"></ul>
				</div>
			</div>
			<div style="float: left; margin-top: 20%;">
				<div class="layui-form-mid layui-word-aux"
					style="margin-left: 100px;">
					<g style="color: red!important;padding:5px;">*</g>
					为必填选项
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">角色名称</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input roleName" name="roleName"
							lay-verify="required" placeholder="请输入角色名称">
					</div>
					<div class="layui-form-mid layui-word-aux">*</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">公司</label>
					<div class="layui-input-inline">
						<select class="comp" name="compId" lay-verify="required">
							<c:forEach items="${compList }" var="list">
								<option value="${list.compId }">${list.compName }</option>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">分成百分比</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input percent" name="percent"
							lay-verify="percent" placeholder="请输入百分比">
					</div>
				</div>

				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label">备注</label>
					<div class="layui-input-block">
						<textarea name="des" placeholder="请输入内容" lay-verify="des"
							class="layui-textarea des"></textarea>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<a class="layui-btn" lay-submit="" lay-filter="addForm">保存</a>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</div>
		</form>
	</div>
</body>

</html>